package controllers;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.mortbay.log.Log;
import org.omg.CORBA.portable.ValueOutputStream;

import models.Notification;
import models.NotificationReceiver;
import models.User;
import models.UserPermission;
import play.mvc.Controller;
import utils.AppConstants;
import utils.C2DM;
import utils.NotificationD;
import utils.NotificationDComplete;
import utils.NotificationReceiverD;

import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * Controller for handling Notification Transactions.
 * 
 * * Please note that throughout this class the terms: "Owner" (or any
 * abbreviation) refers to the person who creates the notification; "Sender" (or
 * any abbreviation) refers to the person who will emit the notification;
 * "Receiver" (or any abbreviation) refers to the person who will be notified.
 * 
 * @author Dalia
 * 
 */
public class ManageNotifications extends Controller {

	/**
	 * Notification Status Constants
	 */
	static final String ACCEPTED = AppConstants.PERMISSIONS[0];
	static final String REJECTED = AppConstants.PERMISSIONS[2];
	static final String PENDING = AppConstants.PERMISSIONS[1];
	
	/**
	 * Notification People Constants
	 */
	static final String UNK = "null";// represents an unregistered receiver

	public ManageNotifications() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Web service for creating a notification
	 * 
	 * @param ntype
	 * @param idOwner
	 * @param phoneSender
	 * @param place
	 * @param start
	 * @param end
	 * @param type
	 * @param when
	 * @param permission
	 * @param timestamp
	 * @param phoneReceivers
	 * 
	 * @author Dalia
	 */
	public static void NewNotification(int ntype, long idOwner,
			String phoneSender, String place, Date start, Date end,
			boolean type, int when, String permission, Date timestamp,
			String phoneReceivers) {
		switch (ntype) {
		case AppConstants.OFFER: // need phoneReceivers
			newOffer(ntype, idOwner, place, type, when, start, end,
					phoneReceivers);
			break;
		case AppConstants.REQUEST: // need permission
			newRequest(ntype, idOwner, phoneSender, place, type, when, start,
					end, permission);
			break;
		case AppConstants.ALARM: // need timestamp
			newAlarm(ntype, idOwner, place, start, end, type, when, timestamp);
			break;
		}
		renderJSON("done");
	}
	
	/**
	 * Creates new notification(s) according to the ntype
	 * @param phoneReceivers: one or more receivers ({phoneReceiver: phoneNumber})
	 * @param phoneSender: one or more senders ({phoneSender: phoneNumber})
	 * @param idOwner: one owner
	 * @param ntype: one type (offer or request or alarm)
	 * @param jsonString: rest of notification info, some/all of:
	 *        (place,type,when,start_date,expiry_date,timestamp,permission)
	 *        For offers: jsonString will contain exactly one notification
	 *        For alarms: jsonString may contain more than one notification
	 */
	@SuppressWarnings("deprecation")
	public static void NewNotification_Generic(String phoneReceivers,
			String phoneSender, long idOwner, int ntype, String jsonString) {
		// initialize variables
		Date start=null,end=null,timestamp=new Date();
		// get variables
		System.out.println("JSON String is "+jsonString);
		try {
			JSONArray data = new JSONArray(jsonString);
			for (int i = 0; i < data.length(); i++) {
				JSONObject js = data.getJSONObject(i);
				String place = js.getString(AppConstants.PLACE_PARAM);
				boolean type = js.getBoolean(AppConstants.TYPE_PARAM);
				int when = js.getInt(AppConstants.WHEN_PARAM);
				System.out.println(place + " " + type + " " + when);
				//try to get dates; might not be available
				try{
					start = new Date(js.getString(AppConstants.START_DATE_PARAM));
				}catch(JSONException je){
					//no start dates was found
				}catch(IllegalArgumentException ie){
					
				}
				try{
					end = new Date(js.getString(AppConstants.END_DATE_PARAM));
				}catch(JSONException je){
					//no end date was found
				}catch(IllegalArgumentException ie){
					
				}
				
				switch (ntype) {
				case AppConstants.OFFER: // need phoneReceivers
					newOffer(ntype, idOwner, place, type, when, start, end,
							phoneReceivers);
					break;
				case AppConstants.REQUEST: 
					newRequest_test(ntype, idOwner, phoneSender, place, type,
							when, start, end);
					break;
				case AppConstants.ALARM: //need time stamp
					newAlarm(ntype, idOwner, place, start, end, type, when,
							timestamp);
					break;
				}
				
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//error msg
		}

	}

	/**
	 * Offers
	 * 
	 * @param ntype
	 * @param idOwner
	 * @param place
	 * @param type
	 * @param when
	 * @param start
	 * @param end
	 * @param phoneReceivers
	 */
	private static void newOffer(int ntype, long idOwner, String place,
			boolean type, int when, Date start, Date end, String phoneReceivers) {
		try {
			User owner = User.getUser(idOwner);
			String phoneSender = owner.getPhone();

			Notification notif = new Notification(ntype, owner, phoneSender,
					place, start, end, type, when);
			notif.insert();
			long notifId = notif.getId();

			// insert receivers in NotificationReceivers table if Offer
			//String[] rec = phoneReceivers.split(";");
			JSONArray rec = new JSONArray(phoneReceivers);
			for (int i=0;i<rec.length();i++) {
				String s = rec.getJSONObject(i).getString(AppConstants.PHONE_RECEIVER_PARAM);
				NotificationReceiver notifRec = new NotificationReceiver(
						notifId, s);
				notifRec.insert();
			}
			// send c2dm to creator to update list
			C2DM.push(phoneSender, AppConstants.CHANGE_OUTGOING_CK,
					AppConstants.CHANGE_OUTGOING_MSG);

			// renderText("Created notification successfully");
			renderJSON(notif);
		} catch (Exception e) {
			// not complete
			e.printStackTrace();
		}
	}

	/**
	 * Requests
	 * 
	 * @param ntype
	 * @param idOwner
	 * @param phoneSender
	 * @param place
	 * @param type
	 * @param when
	 * @param start
	 * @param end
	 */
	private static void newRequest(int ntype, long idOwner, String phoneSender,
			String place, boolean type, int when, Date start, Date end,
			String permission) {
		try {
			User owner = User.getUser(idOwner); // to be changed to phone
			ArrayList<Integer> temp = new ArrayList<Integer>();

			// for multiple senders, create one notification each
			String[] rec = phoneSender.split(";");
			// used for c2dm msgs
			List<String> phones = new LinkedList<String>();
			for (String s : rec) {
				if (checkUser(s)) {
					temp.add(1);
					phones.add(s);
					Notification notif = new Notification(ntype, owner, s,
							place, start, end, type, when, permission);
					notif.insert();
				} else {
					temp.add(0);
				}
			}
			// notify users for new request (who will send their location)
			sendC2DMNewRequest(phones);
			// **notify creator about created request to update his list (better
			// to be with http response)
			C2DM.push(owner.getPhone(), AppConstants.CHANGE_INCOMING_CK,
					AppConstants.CHANGE_INCOMING_MSG);

			boolean[] isRegistered = new boolean[temp.size()];
			for (int i = 0; i < temp.size(); i++) {
				if (temp.get(i).intValue() == 0) {
					isRegistered[i] = false;
					System.out.println("not registered");
				} else {
					isRegistered[i] = true;
					System.out.println("registered");
				}
			}

			renderJSON(isRegistered);
			// renderText("Created notification successfully");

		} catch (Exception e) {
			// not complete
			e.printStackTrace();
		}
	}

	/**
	 * Requests
	 * 
	 * @param ntype
	 * @param idOwner
	 * @param phoneSender
	 * @param place
	 * @param type
	 * @param when
	 * @param start
	 * @param end
	 */
	private static void newRequest_test(int ntype, long idOwner,
			String phoneSender, String place, boolean type, int when,
			Date start, Date end) {
		try {
			User owner = User.getUser(idOwner); // to be changed to phone
			ArrayList<Integer> temp = new ArrayList<Integer>();

			// for multiple senders, create one notification each
			JSONArray reca = new JSONArray(phoneSender);
			//JSONArray places = new JSONArray(place);
			//String[] rec = phoneSender.split(";");
			// used for c2dm msgs
			List<String> phones = new LinkedList<String>();
			for (int i = 0; i < reca.length(); i++) {
				String s = reca.getJSONObject(i).getString(AppConstants.PHONE_RECEIVER_PARAM);
				System.out.println("Phone " + s);
				if (checkUser(s)) {
					System.out.println("ok " + s);
					temp.add(1);
					phones.add(s);
					String permission = AppConstants.PERMISSIONS[
					                     UserPermission.getPermission(owner.getPhone(),s)];
					Notification notif = new Notification(ntype, owner, s,
							place, start, end, type, when, permission);
					notif.insert();
				} else {
					temp.add(0);
				}
			}
			// notify users for new request (who will send their location)
			sendC2DMNewRequest(phones);
			// **notify creator about created request to update his list (better
			// to be with http response)
			C2DM.push(owner.getPhone(), AppConstants.CHANGE_INCOMING_CK,
					AppConstants.CHANGE_INCOMING_MSG);

			boolean[] isRegistered = new boolean[temp.size()];
			for (int i = 0; i < temp.size(); i++) {
				if (temp.get(i).intValue() == 0) {
					isRegistered[i] = false;
					System.out.println("not registered");
				} else {
					isRegistered[i] = true;
					System.out.println("registered");
				}
			}

			renderJSON(isRegistered);
			// renderText("Created notification successfully");

		} catch (Exception e) {
			// not complete
			e.printStackTrace();
		}
	}
	
	/**
	 * Checks if a user is registered so that he can receive a request
	 * to send a notification
	 * @param phone
	 * @return
	 * @author Emy
	 */
	private static boolean checkUser(String phone) {
		System.out.println("checking phone:" + phone);
		User temp = User.findByPhone(phone);
		if (temp == null)
			return false;
		else
			return true;
	}

	/**
	 * Sends c2dm messages to list of phones it works on threads in order not to
	 * handle or stop the webservice
	 * 
	 * @param phones
	 *            list of phones of users that get request from some user
	 * @author mhm
	 */
	private static void sendC2DMNewRequest(final List<String> phones) {
		// new Thread(new Runnable() {
		// @Override
		// public void run() {
		C2DM.pushForgroup(phones, AppConstants.NEW_REQUEST_CK,
				AppConstants.NEW_REQUEST_MSG);
		C2DM.pushForgroup(phones, AppConstants.CHANGE_OUTGOING_CK,
				AppConstants.CHANGE_OUTGOING_MSG);
		// }
		// }).start();
	}

	/**
	 * Alarms
	 * 
	 * @param ntype
	 * @param idOwner
	 * @param place
	 * @param start
	 * @param end
	 * @param type
	 * @param when
	 * @param timestamp
	 */
	private static void newAlarm(int ntype, long idOwner, String place,
			Date start, Date end, boolean type, int when, Date timestamp) {
		try {
			User owner = User.getUser(idOwner);
			String phoneSender = owner.getPhone();

			Notification notif = new Notification(ntype, owner, phoneSender,
					place, start, end, type, when, timestamp);
			notif.insert();

			// send c2dm to creator to update list of self alrams
			C2DM.push(phoneSender, AppConstants.CHANGE_SELF_CK,
					AppConstants.CHANGE_SELF_MSG);

			// renderText("Created notification successfully");
			renderJSON(notif);
		} catch (Exception e) {
			// not complete
			e.printStackTrace();
		}
	}

	private static void SetFlag(Notification r) {
		r.setSent(true);
		r.update();
	}

	/**
	 * Gets all requests that owner with id requested (Incoming to owner)
	 * 
	 * @author Aya Kamel, Dalia
	 * @param id
	 *            : The owner id
	 */
	public static void getIncomingRequestsFor(long id) {
		String tracer = "Tracing\n";
		try {
			User owner = User.getUser(id); // to be changed to phone
			List<Notification> req = Notification
					.getRequestsWhereOwnerIs(owner);
			List<NotificationD> notifications = new ArrayList<NotificationD>();
			// renderJSON(req);
			for (Notification request : req) {
				tracer += "Will start fetching user with phone: "
						+ owner.getPhone() + "\n";
				User u = User.findByPhone(request.getPhoneSender());// get
																	// sender
																	// info
				tracer += "Got user: " + u.toString() + "\n";
				String status = request.getPermission();
				if (status == null)
					status = PENDING;
				tracer += "Set status: " + status + "\n";
				NotificationD notification = new NotificationD(u.getFirstName()
						+ " " + u.getLastName(), status, request.getPlace(), request.getStartDate(),
						request.getExpiryDate(),
						request.isType(), request.getWhen(), request.id,
						u.getPhone(), request.getNtype());
				
				tracer += "Created Request " + notification.toString() + "\n";
				notifications.add(notification);
				tracer += "Added ntype\n";
			}
			// renderText("Number of notifications:"+req.size()+"\n"+NotificationD.print(notifications));
			renderJSON(notifications);
		} catch (Exception e) {
			// not complete
			renderText(tracer + e.toString());
		}
	}

	/**
	 * Gets all offers that owner with id offered (Outgoing from owner)
	 * 
	 * @author Aya Kamel, Dalia
	 * @param id
	 *            : The owner id
	 */
	public static void getOutgoingOffersFrom(long id) {
		String tracer = "Tracing\n";
		try {
			User owner = User.getUser(id); // to be changed to phone
			List<Notification> off = Notification.getOffersWhereOwnerIs(owner);
			List <Notification> req= Notification.getRequestsWhereSenderIs(owner);
			off.addAll(req);
			// List<NotificationD> notifications = new
			// ArrayList<NotificationD>();
			List<NotificationDComplete> notifications = new ArrayList<NotificationDComplete>();
			for (Notification offer : off) {

				String status = offer.getPermission();
				if (status == null)
					status = PENDING;
				tracer += "Set status: " + status + "\n";
				NotificationD notification;
				List<NotificationReceiver> rec = NotificationReceiver
						.getReceiversFor(offer.getId());
				// arrays to hold receivers' info
				String[] allReceivers = new String[rec.size()];
				String[] allFirstName = new String[rec.size()];
				String[] allLastName = new String[rec.size()];
				
				JSONArray nrd = new JSONArray();//array of receivers
				
				if(offer.getNtype()==AppConstants.OFFER){
				// get all receivers
				for (NotificationReceiver r : rec) {
					User u = User.findByPhone(r.getReceiver());
					int index = rec.indexOf(r);
					NotificationReceiverD nr;
					if (u != null) {
						tracer += "User: " + u.toString() + "\n";
						/*
						 * allReceivers+=u.getPhone()+";";
						 * allFirstName+=u.getFirstName()+";";
						 * allLastName+=u.getLastName()+";";
						 */

						allReceivers[index] = u.getPhone();
						allFirstName[index] = u.getFirstName();
						allLastName[index] = u.getLastName();
						nr = new NotificationReceiverD(u.getPhone(), u.getFirstName(), u.getLastName());

					} else {
						tracer += "User: Anonymous \n";
						allReceivers[index] = r.getReceiver();
						allFirstName[index] = UNK;
						allLastName[index] = UNK;
						nr = new NotificationReceiverD(r.getReceiver(), UNK, UNK);
						/*
						 * allReceivers+=r.getReceiver()+";";
						 * allFirstName+=UNK+";"; allLastName+=UNK+";";
						 */
					}
					nrd.put(nr);
				}
				}
				else
					if(offer.getNtype()==AppConstants.REQUEST)
					{
						User u = User.findByPhone(offer.getPhoneOwner());
						NotificationReceiverD nr = new NotificationReceiverD(u.getPhone(), u.getFirstName(), u.getLastName());
						nrd.put(nr);
					}
						

				notification = new NotificationD("" + " " + "", status,
						offer.getPlace(), offer.getStartDate(),offer.getExpiryDate(),
						offer.isType(), offer.getWhen(),
						offer.id, "",offer.getNtype());
				
				//NotificationReceiverD nrd = new NotificationReceiverD(
					//	allReceivers, allFirstName, allLastName);
				NotificationDComplete nd = new NotificationDComplete(
						notification, nrd);
				tracer += "Created Offer " + notification.toString() + "\n";
				// notifications.add(notification);
				notifications.add(nd);
				tracer += "Added Offer\n";
			}
			// renderText("Number of notifications:"+req.size()+"\n"+NotificationD.print(notifications));
			renderJSON(notifications);
		} catch (Exception e) {
			// not complete
			renderText(tracer + e.toString());
		}
	}

	/**
	 * Gets all alarms that owner with id set (self)
	 * 
	 * @author Dalia
	 * @param id
	 *            : The owner id
	 */
	public static void getAlarmsFor(long id) {
		String tracer = "Tracing\n";
		try {
			User owner = User.getUser(id); // to be changed to phone
			List<Notification> alarm = Notification
					.getAlamrsWhereOwnerIs(owner);
			List<NotificationD> notifications = new ArrayList<NotificationD>();
			// renderJSON(req);
			for (Notification a : alarm) {

				tracer += "Got user: " + owner.toString() + "\n";
				String status = a.getPermission();
				if (status == null)
					status = PENDING;
				tracer += "Set status: " + status + "\n";
				NotificationD notification = new NotificationD(
						owner.getFirstName() + " " + owner.getLastName(),
						status, a.getPlace(), a.getStartDate(),a.getExpiryDate(),
						a.isType(), a.getWhen(), a.id,
						owner.getPhone(),a.getNtype());
				tracer += "Created Alarm " + notification.toString() + "\n";
				notifications.add(notification);
				tracer += "Added Alarm\n";
			}
			// renderText("Number of notifications:"+req.size()+"\n"+NotificationD.print(notifications));
			renderJSON(notifications);
		} catch (Exception e) {
			// not complete
			renderText(tracer + e.toString());
		}
	}

	/**
	 * 
	 * @param phoneReceiver
	 * @throws JSONException
	 */
	public static void respondToRequest(String phone)
			throws JSONException {
		try {
			System.out.println(phone);
			List<Notification> list_requests = Notification
					.getAllUnsentRequestsTo(phone);
			System.out.println("hhh"+list_requests.size());
			JSONArray array = new JSONArray();
			for (Notification request : list_requests) {
				User u = User.findByPhone(request.getPhoneSender());
				SetFlag(request);
				NotificationD notification = new NotificationD(u.getFirstName()
						+ " " + u.getLastName(),request.getPermission(), request.getPlace(),
						request.getStartDate(),	request.getExpiryDate(),request.isType(),
						request.getWhen(),request.getId(),request.getPhoneSender(),request.getNtype());
				System.out.println(u.getFirstName() + " " + u.getLastName());
				array.put(notification);
			}
			// notifiy the receiver to update its outgoing list
//			C2DM.push(phoneReceiver, AppConstants.CHANGE_OUTGOING_CK,
//					AppConstants.CHANGE_INCOMING_MSG);
			System.out.println("out"+array.toString());
			renderJSON(array);
		} catch (Exception e) {
			renderText("Error");
		}
	}

	public static void getArrivals(String phone) {
		try {
			User user = User.findByPhone(phone);
			//List<NotificationD> result = new ArrayList<NotificationD>();
			JSONArray result=new JSONArray();
			List<Notification> req = Notification.getRequestsWhereOwnerIs(user);
			List<NotificationReceiver> off = NotificationReceiver
					.getNotificationsWhereReceiverIs(phone);
			List<Notification> alarms = Notification.getAlamrsWhereOwnerIs(user);
			for (Notification request : req) {
				if (request.isFulfilled()) {
					User u = request.getOwner().fetchData();// owner is receiver in requests
					NotificationD notification = new NotificationD(
							u.getFirstName() + " " + u.getLastName(), ACCEPTED,
							request.getPlace(), request.isType(),
							request.getWhen(), request.id, u.getPhone(),
							request.getNtype());
					//result.add(notification);
					result.put(notification);
				}
			}
			//looping on alarms
			for (Notification alarm : alarms) {
				if (alarm.isFulfilled()) {
					User u = alarm.getOwner().fetchData();// owner is receiver in requests
					NotificationD notification = new NotificationD(
							u.getFirstName() + " " + u.getLastName(), ACCEPTED,
							alarm.getPlace(), alarm.isType(),
							alarm.getWhen(), alarm.id, u.getPhone(),
							alarm.getNtype());
					//result.add(notification);
					result.put(notification);
				}
			}
			for (NotificationReceiver offer : off) {
				Notification n = Notification.getNotification(offer
						.getNotif_id());
				if (n.isFulfilled()) {
					User u = user; // user is receiver!!
					NotificationD notification = new NotificationD(
							u.getFirstName() + " " + u.getLastName(), ACCEPTED,
							n.getPlace(),n.getStartDate(),n.getExpiryDate(), n.isType(), n.getWhen(), n.id,
							u.getPhone(), n.getNtype());
					//result.add(notification);
					result.put(notification);
				}
			}
			renderJSON(result);
		} catch (Exception e) {
			renderText("Error");
		}
	}

	/**
	 * Checks if a notification has been fulfilled
	 * 
	 * @param req_id
	 */
	public static void checkFulfilled(long req_id) {
		Notification req = Notification.getNotification(req_id);
		boolean fulfilled = req.isFulfilled();
		renderText(fulfilled);
	}

	/**
	 * Sets the notification fulfilled flag to true.
	 * 
	 * @param notif_id
	 */
	public static void setFulfilled(long notif_id) {
		Notification notif = Notification.getNotification(notif_id);
		notif.setFulfilled(true);
		notif.update();
	}

	/**
	 * Sets the notification fulfilled flag to false.
	 * 
	 * @param notif_id
	 */
	public static void unsetFulfilled(long notif_id) {
		Notification notif = Notification.getNotification(notif_id);
		notif.setFulfilled(false);
		notif.update();
	}

	/**
	 * Change the permission of a ntype to Accepted
	 * 
	 * @param notif_id
	 */
	public static void setAccepted(long notif_id) {
		Notification notif = Notification.getNotification(notif_id);
		notif.setPermission(ACCEPTED);
		notif.update();
		C2DM.push(notif.getPhoneOwner(), AppConstants.CHANGE_INCOMING_CK,
				AppConstants.CHANGE_INCOMING_MSG);
		C2DM.push(notif.getPhoneSender(), AppConstants.CHANGE_OUTGOING_CK,
				AppConstants.CHANGE_OUTGOING_MSG);
	}

	/**
	 * Change the permission of a notification to rejected
	 * 
	 * @param req_id
	 */
	public static void setRejected(long req_id) {
		Notification req = Notification.getNotification(req_id);
		req.setPermission(REJECTED);
		req.update();
		C2DM.push(req.getPhoneOwner(), AppConstants.CHANGE_INCOMING_CK,
				AppConstants.CHANGE_INCOMING_MSG);
		C2DM.push(req.getPhoneSender(), AppConstants.CHANGE_OUTGOING_CK,
				AppConstants.CHANGE_OUTGOING_MSG);
	}

	/**
	 * Cancel (delete everything) the notification with the id "notif_id"
	 * 
	 * @param notif_id
	 * @author Dalia
	 */
	public static void cancelNotification(long notif_id) {
		Notification notif = Notification.getNotification(notif_id);
		if (notif.getNtype() == AppConstants.OFFER) {
			NotificationReceiver.deleteAllReceivers(notif_id);
		}
		notif.delete();
		// notify related user
		sendC2DMAboutCancel(notif);
	}

	private static void sendC2DMAboutCancel(Notification notif) {
		String ownerPhone = notif.getPhoneOwner();
		String senderPhone = notif.getPhoneSender();
		int notifType = notif.getNtype();
		switch (notifType) {

		case AppConstants.OFFER:
			// **notify the owner(sender) (can be updated according to http
			// response)
//			C2DM.push(ownerPhone, AppConstants.CHANGE_OUTGOING_CK,
//					AppConstants.CHANGE_OUTGOING_MSG);
			break;
		case AppConstants.REQUEST:
			// notify the receiver (the owner)
			C2DM.push(ownerPhone, AppConstants.CHANGE_INCOMING_CK,
					AppConstants.CHANGE_INCOMING_MSG);
			// notify the sender
			C2DM.push(senderPhone, AppConstants.CHANGE_OUTGOING_CK,
					AppConstants.CHANGE_OUTGOING_MSG);
			break;
		case AppConstants.ALARM:
			// **notify the user itself (can be handled by http response)
//			C2DM.push(ownerPhone, AppConstants.CHANGE_SELF_CK,
//					AppConstants.CHANGE_SELF_MSG);
			break;

		}

	}

	/**
	 * Delete the notification with the id "notif_id"
	 * 
	 * @param notif_id
	 * @author Dalia
	 */
	public static void deleteNotification(long notif_id, String phone) {
		Notification notif = Notification.getNotification(notif_id);
		if (notif.getNtype() != AppConstants.OFFER) {
			notif.delete();
			return;
		} else {// offers must delete receivers first

			NotificationReceiver.deleteReceiver(notif_id, phone);
			// delete notif itself when no receivers are left
			if (NotificationReceiver.getReceiversFor(notif_id).size() == 0)
				notif.delete();
		}
		// update related users
		sendC2DMAboutCancel(notif);
	}
	
}
